<?php
namespace app\index\controller;
use app\index\controller\Home;
use think\Request;
use think\helper\Time;
class Index extends Home
{
    /**
     * fly社区首页
     * @return \think\response\View
     */
    public function index()
    {
        $where = [];
    	$this->assign('title','FLY社区');
    	
    	//查询所有的帖子分类
    	$cate = model("category");
    	$cateInfo = $cate->queryAllCate(['id','category_name']);
    	$this->assign('cateInfo',$cateInfo);
//        dump(input("get.action"));
//        dump(Request::instance()->param("action"));
//        die;
        $action = empty(Request::instance()->param("action"))? "byTime":"input('get.action')";

        //精华帖
        if(!empty(Request::instance()->param("fine"))){
            $where["is_fine"] = 1;
            $this->assign('fine',"fine");
        }else{
            $this->assign('fine',"");
        };

        //帖子类别
        if(!empty(Request::instance()->param("category"))){
            switch(Request::instance()->param("category")){
                case 1:
                    $where["category_id"] = 1;
                    $this->assign('category_id',1);
                    break;
                case 2:
                    $where["category_id"] = 2;
                    $this->assign('category_id',2);
                    break;
                case 3:
                    $where["category_id"] = 3;
                    $this->assign('category_id',3);
                    break;
                case 4:
                    $where["category_id"] = 4;
                    $this->assign('category_id',4);
                    break;
                case 5:
                    $where["category_id"] = 5;
                    $this->assign('category_id',5);
                    break;
                case 6:
                    $where["category_id"] = 6;
                    $this->assign('category_id',6);
                    break;
            }
            $this->assign('category',"category");
        }else{
            if(empty($where['category_id'])){
                $this->assign('category',"no");
                $this->assign('category_id',"");
            }
        };


        $card = model("card");

        //通过时间或者评论数量
        if($action == "byTime"){
            //查询所有的帖子 默认通过发帖时间
            $cardList = $card -> queryAllCardByTime($where);
            $this->assign('action',"byTime");
        }else{
            //查询所有的帖子 通过评论数量
            $cardList = $card -> queryAllCardByEvaluate($where);
            $this->assign('action',"byEvaluate");
        }


        //查询我的金币数量
        $user_id = session('home.id');
        $user= db('user')->where('id',$user_id)->find();
        $gold_num = $user['gold'];
        $this->assign('gold_num',$gold_num);
    

        //回帖周榜
        // $a = Time::lastWeek();
        // $start = date("Y-m-d H:i:s",$a[0]);
        // $end = date("Y-m-d H:i:s",$a[1]);
        $start = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")));  
        $end = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
        // dump($start);
        // dump($end);
        // dump($a);
        // die;
        $timeList = db('user')
        ->field('u.*,e.*,e.create_time etime,count(e.user_id) count')
        ->alias('u')
        ->join('fly_evaluate e','u.id = e.user_id')
        // ->order(' desc')
        ->where('e.create_time','between',[$start,$end])
        ->group('e.user_id')
        ->limit(12)
        ->order('count(e.user_id) desc')
        ->select();
        
        $this->assign('timeList',$timeList);
        // dump($timeList);die;



        //查询本周热议
        $hotList = db('card')
        // ->alias('c')
        // ->join('fly_evaluate e','c.id = e.card_id')
        ->order('browse desc')
        ->limit(5)
        ->select();
        // dump($hotList);die;
        $this->assign('hotList',$hotList);

        //查询友情链接
        $linkList = db('link')->where('is_show',1)->select();
        $this->assign('linkList',$linkList);

        $this->assign('cardList',$cardList['data']);
        $this->assign('cardPage',$cardList['page']);
//        dump($cardList);die;
        //置顶帖子查询
        $cardFirst = $card->queryAllCardByFirst();
        $this->assign('cardFirst',$cardFirst);
        //如果为空,则进入首页

        return view('Index/index');


    }


    public function qd()
    {
        $t = time();
        $start_time = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t));  //当天开始时间
        $end_time = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t)); //当天结束时间
        $start_time = date('Y-m-d H:i:s',$start_time);
        $end_time = date('Y-m-d H:i:s',$end_time);
        //查看是否签到,如果没签到则签到
        $user_id = session('home.id');

        $res = db('user')
        ->alias('u')
        ->join('fly_sign s','u.id=s.user_id')
        ->where('s.create_time','between',[$start_time,$end_time])
        ->where('s.user_id',$user_id)
        ->select();
        // dump($res);
        if($res==null){
            //如果为null,则可以签到
            //签到
            $data = ['user_id' => $user_id , 'gold' => 5];
            $res = db('sign')->insert($data);
            //插入我的用户表
            db('user')->where('id', $user_id)->setInc('gold', 5);
            if($res > 0){
                //成功添加
                $data['status'] = 1;
                return json($data);
            }else{
                //未知错误
                $data['status'] = -1;
                return json($data);
            }
        }else{
            //不为null,则已经签过了
            // 已经签到过;
            $data['status'] = 0;
            return json($data);

        }



        // $timeList = db('user')
        // ->field('u.*,e.*,e.create_time etime,count(e.user_id) count')
        // ->alias('u')
        // ->join('fly_evaluate e','u.id = e.user_id')
        // // ->order(' desc')
        // ->where('e.create_time','between',[$start,$end])
        // ->group('e.user_id')
        // ->limit(12)
        // ->order('count(e.user_id) desc')
        // ->select();





    }

    

}
